package com.zhonghan.recharge.service.impl;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhonghan.common.bean.Result;
import com.zhonghan.common.enums.ResultStatus;
import com.zhonghan.common.utils.StringUtils;
import com.zhonghan.recharge.dao.ZhFillCardDao;
import com.zhonghan.recharge.pojo.CustomerCard;
import com.zhonghan.recharge.pojo.ZhFillCard;
import com.zhonghan.recharge.service.ZhFillCardService;
@Service
public class ZhFillCardServiceImpl implements ZhFillCardService{
    @Autowired
    private ZhFillCardDao zhFillCardDao;
    @Override
    public long getZhFillCardRowCount(){
        return zhFillCardDao.getZhFillCardRowCount();
    }
    @Override
    public List<ZhFillCard> selectZhFillCard(){
        return zhFillCardDao.selectZhFillCard();
    }
    @Override
    public List<ZhFillCard> selectZhFillCardByObj(ZhFillCard obj){
        return zhFillCardDao.selectZhFillCardByObj(obj);
    }
    @Override
    public ZhFillCard selectZhFillCardById(Integer id){
        return zhFillCardDao.selectZhFillCardById(id);
    }
    @Override
    public ZhFillCard selectZhFilLCardByCardNo(Integer sequence){
    	return zhFillCardDao.selectZhFillCardByCardNo(sequence);
    }
    
    @Override
    public int insertZhFillCard(ZhFillCard value){
        return zhFillCardDao.insertZhFillCard(value);
    }
    @Override
    public int insertNonEmptyZhFillCard(ZhFillCard value){
        return zhFillCardDao.insertNonEmptyZhFillCard(value);
    }
    @Override
    public int insertZhFillCardByBatch(List<ZhFillCard> value){
        return zhFillCardDao.insertZhFillCardByBatch(value);
    }
    @Override
    public int deleteZhFillCardById(Integer id){
        return zhFillCardDao.deleteZhFillCardById(id);
    }
    @Override
    public int updateZhFillCardById(ZhFillCard enti){
        return zhFillCardDao.updateZhFillCardById(enti);
    }
    @Override
    public int updateNonEmptyZhFillCardById(ZhFillCard enti){
        return zhFillCardDao.updateNonEmptyZhFillCardById(enti);
    }

    public ZhFillCardDao getZhFillCardDao() {
        return this.zhFillCardDao;
    }

    public void setZhFillCardDao(ZhFillCardDao zhFillCardDao) {
        this.zhFillCardDao = zhFillCardDao;
    }
    
    /**
     * 查询充值卡条件查询,带分页
     */
    public Result selectCardList(String sequence,Integer status,Integer facePrice
    		,String beginTime,String endTime,Integer pageNum,Integer pageSize){
    	
    	pageNum = pageNum == null ? 0 : pageNum;
    	pageSize = pageSize == null ? 0 : pageSize;
    	
    	ZhFillCard zhFileCard =new ZhFillCard();
    	
    	if(StringUtils.isNotEmpty(sequence)){
    		zhFileCard.setSequence(sequence);
    	}
    	if(StringUtils.isNotEmpty(beginTime)){
    		zhFileCard.setBeginTime(beginTime);
		}
		if(StringUtils.isNotEmpty(endTime)){
			zhFileCard.setEndTime(endTime);
		}
		
    	zhFileCard.setStatus(status);
    	zhFileCard.setFacePrice(facePrice);
    	
    	//设置卡号升序
    	zhFileCard.setOrderBy("sequence");
    	zhFileCard.setSortBy("asc");
//    	List<ZhFillCard> zhFileCardList=zhFillCardDao.selectCardList(zhFileCard);
    	Result result = new Result();
		try {
			
			PageHelper.startPage(pageNum, pageSize);
			List<ZhFillCard> zhFileCardList=zhFillCardDao.selectCardList(zhFileCard);
			PageInfo<ZhFillCard> pageInfo = new PageInfo<>(zhFileCardList);
			result.setCode(ResultStatus.SUCCESS.getCode());
			result.setData(pageInfo);
			
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(ResultStatus.BUSYNESS.getCode());
			result.setMsg(ResultStatus.BUSYNESS.getDescription());
		}
    	return result;
    }
	@Override
	public long getZhFillCardRowCountByObj(ZhFillCard obj) {
		
		return zhFillCardDao.getZhFillCardRowCountByObj(obj);
	}
	
    public List<ZhFillCard> getZhFillCardByObj(ZhFillCard obj){
    	return zhFillCardDao.getZhFillCardByObj(obj);
    }
    /**
     * 激活某个卡的阶段
     * @param sequenceBegin 卡开始的阶段
     * @param sequenceEnd 卡结束的阶段
     * @return
     */
    public int updateZhFillCardByStatus(String sequenceBegin,String sequenceEnd){
    	int newStatus = 0;
    	int oldStatus = -1;
    	return zhFillCardDao.updateZhFillCardByStatus(sequenceBegin, sequenceEnd,oldStatus,newStatus);
    }
    
    
	@Override
	public Result getCustomerCardList(String beginTime, String endTime, Integer status, String customerName,Integer pageNum, Integer pageSize) {
		pageNum = pageNum == null ? 1 : pageNum;
		pageSize = pageSize == null ? 10 :pageSize;
		Result result = new Result();
		
		/*SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if(StringUtils.isEmpty(beginTime) && StringUtils.isEmpty(endTime)){
			beginTime = sdf.format(new Date());
			endTime = beginTime;
		}*/
		
		try {
			PageHelper.startPage(pageNum, pageSize);
			List<CustomerCard> list = zhFillCardDao.getCustomerCardList(beginTime,endTime,status,customerName);
			PageInfo<CustomerCard> pageInfo = new PageInfo<>(list);
			result.setCode("1");
			result.setData(pageInfo);
		} catch (Exception e) {
			result.setFailure("-1", "系统繁忙,请稍后再试!");
			e.printStackTrace();
		}
		
		return result;
	}
}